package com.example.robertluozizhao.baseframedemo;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.robertluozizhao.baseframedemo.base.BaseActivity;
import com.example.robertluozizhao.baseframedemo.ui.fragment.HomeFragment;
import com.example.robertluozizhao.baseframedemo.ui.fragment.MangerFragment;
import com.example.robertluozizhao.baseframedemo.ui.fragment.PersonFragment;

import butterknife.Bind;


public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {


    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        super.initView();
        initToolBar(); // 标题栏
        initBottomNavBar();
        initTab();
    }

    private void initToolBar() {
        mTitle.setText(R.string.bottom_nav_home);
    }

    @Override
    public void initData() {
        super.initData();
        bottomNavigationBar.setTabSelectedListener(this);
    }


    private BottomNavigationBar bottomNavigationBar;
    private HomeFragment homeFragment;
    private MangerFragment mangerFragment;
    private PersonFragment personFragment;


    private void initBottomNavBar() { /*1.首先进行fvb*/
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_nav_bar); /*2.进行必要的设置*/
        bottomNavigationBar.setBarBackgroundColor(R.color.white);   // 导航栏字体颜色
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);//适应大小
         /*3.添加Tab*/
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, R.string.bottom_nav_home)
                        .setInactiveIconResource(R.mipmap.ic_launcher).setActiveColorResource(R.color.littleblue))

                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, R.string.bottom_nav_manager)
                        .setInactiveIconResource(R.mipmap.ic_launcher).setActiveColorResource(R.color.littleblue))

                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher, R.string.bottom_nav_person)
                        .setInactiveIconResource(R.mipmap.ic_launcher).setActiveColorResource(R.color.littleblue))
                .setFirstSelectedPosition(0) //默认显示面板
                .initialise();  //初始化
    }


    @Override
    public void onTabSelected(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                }
                mTitle.setText(R.string.bottom_nav_home);
                transaction.replace(R.id.content_main, homeFragment);
                break;
            case 1:
                if (mangerFragment == null) {
                    mangerFragment = new MangerFragment();
                }
                mTitle.setText(R.string.bottom_nav_manager);
                transaction.replace(R.id.content_main, mangerFragment);
                break;
            case 2:
                if (personFragment == null) {
                    personFragment = new PersonFragment();
                }
                mTitle.setText(R.string.bottom_nav_person);
                transaction.replace(R.id.content_main, personFragment);
                break;
        }
        transaction.commit();
    }


    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }


    private void initTab() {
        homeFragment = new HomeFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content_main, homeFragment);
        transaction.commit();
    }

}
